// pages/community/info.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      id:0,
      from:5,
      datainfo:{},
      pingLunData:{isShowPingLun:0,value:'',isshuru:0,yifa:0},
      isShowPingLun:1,
      pingLunContent:'',
      bookImage:['1','1','1'],
      pinglunlists:[],
      liulannum:0,
      isShare:0
  },
    hidePingLun:function(){
        this.setData({
            pingLunData:{isShowPingLun:0}
        })
    },

    morehuifu:function(e){
        var id = e.currentTarget.dataset.id;
        var path = '/pages/community/huiFuList?id='+id;
        wx.navigateTo({
            url: path
        })
    },

    showPingLun:function(e){
        var that = this;
        this.setData({
            pingLunData:{isShowPingLun:1,yifa:0},
            pingLunContent:'',
            // id:e.currentTarget.dataset.id,
            from:e.currentTarget.dataset.from
        })
        if(7 == e.currentTarget.dataset.from){
            this.setData({
                pinglunid:e.currentTarget.dataset.id
            })
        }else{
            this.setData({
                id:e.currentTarget.dataset.id
            })
        }
    },

    pingluninput:function(e){
        var that = this;
        this.setData({
            pingLunContent:e.detail.value
        })
    },
    fasong:function(e){
        this.setData({
            "pingLunData.yifa":1
        })
        getApp().globalData.iswangchengpinglun=0;
        var that = this;
        var id = 0;
        if(7 == that.data.from){
            id = that.data.pinglunid;
        }else{
            id = that.data.id;
        }
        var formdata = {
            pingLunContent:this.data.pingLunContent,
            uid:getApp().globalData.userInfo.uid,
            otherid:id,
            from:that.data.from
        }

        var i = setInterval(function(e){
            // console.log(getApp().globalData.iswangchengpinglun);
            if(1 == getApp().globalData.iswangchengpinglun){
                that.hidePingLun();
                that.getDataInfo();
                clearInterval(i);
            }
        },1000)
        // console.log(JSON.stringify(formdata))
        app.pinglun(formdata);
        that.setData({
            pingLunContent:''
        })
    },
    onLoad:function(options){
        var that = this;
        this.setData({
            id:options.id
        })
        this.getDataInfo();
        this.upliulan();
        if(undefined != options.isShare){
            this.setData({
                isShare:options.isShare
            })
        }
    },
    //更新浏览量
    upliulan:function(e){
        var that = this;
        wx.request({
            url: getApp().data.server+'/index.php/Tongcheng/Book/liuLanNum?from=3&id='+that.data.id,
            success:function(d){
                // console.log('123'+JSON.stringify(d.data));
                // wx.hideLoading();
                // if(0 == d.data){
                //     return;
                // }
                that.setData({
                    liulannum:d.data
                })
            }
        })
    },
    getDataInfo:function(e){
        var that = this;
        wx.request({
            url: getApp().data.server+'/index.php/Tongcheng/Book/getSheQuanInfo?id='+that.data.id+'&uid='+getApp().globalData.userInfo.uid,
            success:function(d){
                // console.log('123123'+JSON.stringify(d.data))
                that.setData({
                    datainfo:d.data
                })
            }
        })
    },
    //点赞
    dianzan:function(e){
        var that = this;
        var from = e.currentTarget.dataset.from;
        var id = e.currentTarget.dataset.id;
        var formdata = {
            otherid:id,
            from:from
        }
        app.dianZan(formdata);
        //修改当前数据源，避免重新请求数据
        //3社区的书评 4社区的话题 5社区的问答 6社区的写文
        if(1 == that.data.datainfo.isDianZan){
            that.setData({
                'datainfo.dianzan_num':--that.data.datainfo.dianzan_num,
                'datainfo.isDianZan':0
            })
        }else if(0 == that.data.datainfo.isDianZan){
            that.setData({
                'datainfo.dianzan_num':++that.data.datainfo.dianzan_num,
                'datainfo.isDianZan':1
            })
        }
    },
    shouye:function(e){
        wx.reLaunch({
            url: '/pages/find/find'
        })
    },
    onShareAppMessage: function (res) {
        // if (res.from === 'button') {
        //   // 来自页面内转发按钮
        //   console.log(res.target)
        // }
        var id = res.target.dataset.id;
        var path = '/pages/community/wenda/info?isShare=1&id='+id;
        return {
            title: '分享',
            path: path
        }
    },
    bindfocus:function(e){
        var that = this;
        console.log('bindfocus')
        that.setData({
            "pingLunData.isshuru":1
        })
    },
    bindblur:function(e){
        var that = this;
        console.log('bindfocus')
        that.setData({
            "pingLunData.isshuru":0
        })
    }

})